package schema

var (
	suffixMatchHandlerInstance = &SuffixMatchHandler{DefaultMatchNode{Rules: initSuffixMatchRules()}}
	prefixMatchHandlerInstance = &PrefixMatchHandler{DefaultMatchNode{Rules: initPrefixMatchRules()}}
	COLUMN_SCHEMA_GENERATOR    = &FullMatchHandler{DefaultMatchNode{Rules: initFullMatchRules()}}
)

func init() {
	// 设置优先级(全匹配 -> 前缀匹配 -> 后缀匹配)
	COLUMN_SCHEMA_GENERATOR.SetNext(prefixMatchHandlerInstance, prefixMatchHandlerInstance)
	prefixMatchHandlerInstance.SetNext(suffixMatchHandlerInstance, suffixMatchHandlerInstance)
}

func initFullMatchRules() []*DefaultGenerateRule {
	return []*DefaultGenerateRule{
		{Payload: "id", Fn: func(c *TableColumn, err error) error {
			return nil
		}},
		{Payload: "code", Fn: func(c *TableColumn, err error) error {
			if c.Width == 0 {
				c.Width = 140
			}
			if !c.Require {
				c.Require = true
			}
			if !c.Search {
				c.Search = true
			}
			if !c.ShowOverflowTooltip {
				c.ShowOverflowTooltip = true
			}
			if c.Align != "" {
				c.Align = "left"
			}
			if c.Fixed != "" {
				c.Fixed = "left"
			}
			return nil
		}},
	}
}
func initPrefixMatchRules() []*DefaultGenerateRule {
	return []*DefaultGenerateRule{
		{Payload: "is_", Fn: func(c *TableColumn, err error) error {
			if c.Width == 0 {
				c.Width = 60
			}
			if !c.Require {
				c.Require = true
			}
			if !c.IsSwitch {
				c.IsSwitch = true
			}
			if c.Align != "" {
				c.Align = "center"
			}
			return nil
		}},
	}
}
func initSuffixMatchRules() []*DefaultGenerateRule {
	return []*DefaultGenerateRule{
		{Payload: "_img", Fn: func(c *TableColumn, err error) error {
			if c.Width == 0 {
				c.Width = 100
			}
			if !c.IsImg {
				c.IsSwitch = true
			}
			if c.Align != "" {
				c.Align = "center"
			}
			return nil
		}},
	}
}
